﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class FrmSR : Form
    {
        public FrmSR()
        {
            InitializeComponent();
        }
        //名目文件路径
        string itemPath = CommonHelper.itemPath;
        //收入记录文件路径
        string filePath = CommonHelper.incomePath;
        int actType = 1;//提交状态  1 新增   2 修改
        ListViewItem curItem;//当前选择项
        int selIndex = -1;//选择项的索引
        int lastId = 0;//最后一条记录的编号
                       //收入列表
        List<IncomeInfo> incomeList = new List<IncomeInfo>();
        private void btSeaaarchAll_Click(object sender, EventArgs e)
        {
            textBox4.Text = "";
            textBox2.Text = "";
            comboBox2.SelectedIndex = 0;
            textBox3.Text = "";
            listViewData.Items.Clear();
            foreach (IncomeInfo item in incomeList)
            {
                ListViewItem li = CreateLvItem(item);
                listViewData.Items.Add(li);
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {

            //信息接收
            int id = int.Parse(tbNo.Text.Trim());
            string itemName = comboBox1.Text.Trim();
            string remark = tbRemark.Text.Trim();
            decimal money = 0.00M;
            decimal.TryParse(tbMoney.Text.Trim(), out money);
            string incomeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //信息检查
            if (string.IsNullOrEmpty(remark))
            {
                MessageHelper.Error("收入记录提交", "收入记录描述不能空！");
                tbRemark.Focus();
                return;
            }
            if (money <= 0)
            {
                MessageHelper.Error("收入记录提交", "收入金额不能小于0！");
                tbMoney.Focus();
                return;
            }
            //信息封装
            IncomeInfo incomeInfo = new IncomeInfo()
            {
                IncomeId = id,
                ItemName = itemName,
                IncomeDesp = remark,
                Money = money,
                IncomeTime = incomeTime
            };
            //创建ListViewItem项
            ListViewItem li = CreateLvItem(incomeInfo);
            //提交
            if (actType == 1)//新增
            {
                if (!File.Exists(filePath))
                {
                    TextHelper.WriteMsg("编号;名目;描述;金额;记录时间", filePath, false);
                }
                //写入文件
                TextHelper.WriteMsg(id + ";" + itemName + ";" + remark + ";" + money.ToString("0.00") + ";" + incomeTime, filePath, true);
                //添加到listViewData
                listViewData.Items.Add(li);
                //添加到收入记录数据列表中
                incomeList.Add(incomeInfo);
                lastId = id;
            }
            else //修改
            {
                //修改后的收入信息
                listViewData.Items.Insert(selIndex, li);
                //替换原来位置处的收入信息
                incomeList[selIndex] = incomeInfo;
                //移除原来的信息
                listViewData.Items.RemoveAt(selIndex + 1);
            }
        }

        private void btclear_Click(object sender, EventArgs e)
        {
            tbNo.Text = (lastId + 1).ToString();
            comboBox1.SelectedIndex = 0;
            tbRemark.Text = "";
            tbMoney.Text = "";
            if (actType != 1)
            {
                actType = 1;
                btAdd.Text = "新增";
            }

        }

        private void btSave_Click(object sender, EventArgs e)
        {

            if (actType != 1 && incomeList.Count > 0)
            {
                List<string> msgList = new List<string>();
                TextHelper.WriteMsg("编号;名目;描述;金额;记录时间", filePath, false);
                int id = 0;
                listViewData.Items.Clear();
                foreach (IncomeInfo info in incomeList)
                {
                    id += 1;
                    info.IncomeId = id;
                    string val = id + ";" + info.ItemName + ";" + info.IncomeDesp + ";" + info.Money.ToString("0.00") + ";" + info.IncomeTime;
                    msgList.Add(val);
                    listViewData.Items.Add(CreateLvItem(info));
                }
                TextHelper.WriteMsgList(msgList, filePath, true);
                MessageHelper.Info("保存收入记录", "收入记录信息保存成功！");
            }



        }

        private void btdelete_Click(object sender, EventArgs e)
        {
            //选择的项集合
            var selList = listViewData.SelectedItems;
            if (selList.Count > 0)
            {
                DialogResult dr = MessageHelper.Confirm("删除收入记录", "你确定要删除选择收入记录信息吗？");
                if (dr == DialogResult.OK)
                {
                    List<ListViewItem> selItems = new List<ListViewItem>();
                    foreach (ListViewItem item in selList)
                    {
                        selItems.Add(item);
                    }
                    //移除选择的项
                    int i = 0;
                    while (i < selItems.Count)
                    {
                        //获取要移除的项的索引
                        int index = listViewData.Items.IndexOf(selItems[i]);
                        //从列表视图中移除
                        listViewData.Items.Remove(selItems[i]);
                        //从收入数据列表中移除
                        incomeList.RemoveAt(index);
                        if (curItem == selItems[i])
                        {
                            //清空信息栏
                            tbNo.Text = "";
                            comboBox1.SelectedIndex = 0;
                            tbRemark.Text = "";
                            tbMoney.Text = "";
                            if (actType != 1)
                            {
                                actType = 1;
                                btAdd.Text = "新增";
                            }
                            curItem = null;
                        }
                        i++;
                    }
                    actType = 3;//删除
                    lastId = incomeList[incomeList.Count - 1].IncomeId;//重新赋值最后一个编号值
                    MessageHelper.Info("删除收入记录", "选择的收入记录信息删除成功！");
                }
            }
        }

        private void btsearch_Click(object sender, EventArgs e)
        {
            //条件接收
            string kRemark = textBox3.Text.Trim();
            string fItemName = comboBox2.Text.Trim();
            if (fItemName == "请选择")
                fItemName = "";
            decimal stMoney = 0, etMoney = 0;
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                decimal.TryParse(textBox2.Text, out stMoney);
            }
            if (!string.IsNullOrEmpty(textBox4.Text))
            {
                decimal.TryParse(textBox4.Text, out etMoney);
            }
            if (etMoney > 0 && etMoney < stMoney)
            {
                MessageHelper.Error("查询收入记录", "结束范围金额不能小于开始范围金额！");
                textBox4.Focus();
                return;
            }
            IEnumerable<IncomeInfo> findList = incomeList;
            //筛选工作
            if (!string.IsNullOrEmpty(kRemark))
            {
                findList = findList.Where(r => r.IncomeDesp.Contains(kRemark));
            }
            if (!string.IsNullOrEmpty(fItemName))
            {
                findList = findList.Where(r => r.ItemName == fItemName);
            }
            if (stMoney > 0)
            {
                findList = findList.Where(r => r.Money >= stMoney);
            }
            if (etMoney > 0)
            {
                findList = findList.Where(r => r.Money <= etMoney);
            }
            //查询筛选完成后的结果
            //如果有数据，才加载 
            if (findList.Count() > 0)
            {
                listViewData.Items.Clear();
                foreach (IncomeInfo item in findList)
                {
                    ListViewItem li = CreateLvItem(item);
                    listViewData.Items.Add(li);
                }
            }
            else
            {
                listViewData.Items.Clear();
            }
        }

        private void FrmSR_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(itemPath) || string.IsNullOrEmpty(filePath))
            {
                MessageHelper.Error("收入加载", "文件路径并没有配置！");
                //禁用界面功能
                foreach (Control c in this.Controls)
                {
                    c.Enabled = false;
                }
                return;
            }
            //加载收入名目列表
            LoadCboIncomeItems();
            listViewData.Items.Clear();
            //加载已有的收入记录
            LoadIncomeList();
        }
        /// <summary>
        /// 加载收入名目列表
        /// </summary>
        private void LoadCboIncomeItems()
        {
            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            List<string> items = CommonHelper.GetItems(1);
            if (items.Count > 0)
            {
                foreach (string item in items)
                {
                    comboBox1.Items.Add(item);
                    comboBox2.Items.Add(item);
                }
                comboBox2.Items.Insert(0, "请选择");
                comboBox2.SelectedIndex = 0;
                comboBox1.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// 加载已有的收入记录
        /// </summary>
        private void LoadIncomeList()
        {
            List<IncomeInfo> incomeData = CommonHelper.GetIncomeList();
            if (incomeData.Count > 0)
            {
                int id = 0;
                foreach (IncomeInfo item in incomeData)
                {
                    id += 1;
                    ListViewItem li = new ListViewItem();
                    li.Text = item.IncomeId.ToString();
                    li.SubItems.Add(item.ItemName);
                    li.SubItems.Add(item.IncomeDesp);
                    li.SubItems.Add("￥" + item.Money.ToString("0.00"));
                    li.SubItems.Add(item.IncomeTime);
                    listViewData.Items.Add(li);
                    incomeList.Add(item);
                    //记录最后一个Id
                    if (id == incomeData.Count)
                    {
                        lastId = item.IncomeId;
                    }
                }
            }
            tbNo.Text = (lastId + 1).ToString();//自动生成编号
        }
        /// <summary>
        /// 根据收入信息生成列表项
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private ListViewItem CreateLvItem(IncomeInfo info)
        {
            ListViewItem li = new ListViewItem();
            li.Text = info.IncomeId.ToString();
            li.SubItems.Add(info.ItemName);
            li.SubItems.Add(info.IncomeDesp);
            li.SubItems.Add("￥" + info.Money.ToString("0.00"));
            li.SubItems.Add(info.IncomeTime);
            return li;
        }

        private void listViewData_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewData.SelectedItems.Count > 0)
            {
                var selItem = listViewData.SelectedItems[0];
                actType = 2;//修改
                curItem = selItem;//当前选择项  ListVIewItem
                tbNo.Text = curItem.Text;
                comboBox1.Text = curItem.SubItems[1].Text;
                tbRemark.Text = curItem.SubItems[2].Text;
                tbMoney.Text = curItem.SubItems[3].Text.Substring(1);//不显示￥在文本框
                btAdd.Text = "修改";
                selIndex = listViewData.SelectedIndices[0];//选择项索引
            }
        }
    }
}
